package springTest;

/**
 * FX项目中，经常需要近乎实时地为客户提供外汇新闻
 * 
 * @author Administrator
 *
 */
public class FXNewsProvider {
	private IFXNewsListener newsListener;
	private IFXNewsPersister newPersistener;
	
	public FXNewsProvider() {
		// 接口 具体的实现类
		newsListener = new DowJonesNewsListener();//影响重构，比如又搞定一家新闻社，使用该构造方法-耦合，而使用IoC可以解决
		newPersistener = new DowJonesNewsPersister();
	}
	//构造方法注入
	public FXNewsProvider(IFXNewsListener newsListner,IFXNewsPersister newsPersister)
	{
		this.newsListener = newsListner;
		this.newPersistener = newsPersister;
		
	}
	
	
	//Setter方法注入
	public IFXNewsListener getNewsListener() {
		return newsListener;
	}
	public void setNewsListener(IFXNewsListener newsListener) {
		this.newsListener = newsListener;
	}
	public IFXNewsPersister getNewPersistener() {
		return newPersistener;
	}
	public void setNewPersistener(IFXNewsPersister newPersistener) {
		this.newPersistener = newPersistener;
	}
	public void getAndPersistNews() {// 获取、存储新闻
		String[] newsIds = newsListener.getAvailableNewsIds();
		if (ArrayUtils.isEmpty(newsIds))// 非空判断
		{
			return;
		}
		for (String newsId : newsIds) {
			FXNewsBean newsBean = newsListener.getNewsByPK(newsId);// 根据id获取新闻内容
			newPersistener.persistNews(newsBean);// 存储新闻内容
			newsListener.postProcessIfNecessary(newsId);// 处理新闻
		}
	}
}
